<?php
/*
	本插件：$this->model('model')->方法名();
	其他地方调用：model('jy_51job/model')->方法名();
*/
namespace jy_51job;
defined('BY_JYA') or exit('error');
class jianli extends \plugin{
	public function add($postdata,$storeid=0,$noexi=false,$canadd=false){
		if(!$postdata['name']){
			if($noexi){
				return false;
			}
			exi('请填写姓名！','error');
		}
		$data=$postdata;
		$id=intval($postdata['id']);
		if($id){
			pdo_update('jy_51job_jianli',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}else{
			$data['uniacid']=$_SESSION['uniacid'];
			$data['status']=1;
			$data['createtime']=TIMESTAMP;
			pdo_insert('jy_51job_jianli',$data);
		}
		return $id;
	}
	public function daoru($post=array()){
		if(!$post['excel']){
			exi('请先上传excel文件！','error');
		}
		$localexcel=cfc('files')->local($post['excel']);
		$excel=cfc('tools')->importExcel2($localexcel);
		//print_r($excel);exit;
		$excel[1]=$excel[2];
		unset($excel[2]);
		$res=$this->model('model')->get_csvitems($excel);
		if(!$res[0]['姓名'] && !$res[0]['手机号']){
			$excel=cfc('tools')->importExcel2($localexcel,true,array(0),3);
			$excel[1]=$excel[2];
			unset($excel[2]);
			$res=$this->model('model')->get_csvitems($excel);
			if(!$res[0]['姓名'] && !$res[0]['手机号']){
				cfc('files')->source_del($post['excel']);
				exi('导入文件异常，内容为空！','error');
			}
		}
		//print_r($res);exit;
		cfc('files')->source_del($post['excel']);
		$cdata=$this->csv_add($res);
		if($cdata){
			cache_write('jianlidata',$cdata,'jy_51job');
			exi('导入成功（简历重复的会自动忽略）！','','/cdata/');
		}else{
			exi('导入成功！','','/jianli/');
		}
	}
	public function csv_add($rows){
		$cdata=array();
		foreach($rows as $item){
			if(!$item['姓名']){
				continue;
			}
			if(trim($item['期望行业'])){
				$pid=pdo_getcolumn('jy_51job_job_group',array('title'=>trim($item['期望行业']),'uniacid'=>$_SESSION['uniacid']),'id');
				if(!$pid){
					$newgroup=array(
						'uniacid'=>$_SESSION['uniacid'],
						'title'=>trim($item['期望行业']),
						'status'=>1,
						'createtime'=>time()
					);
					pdo_insert('jy_51job_job_group',$newgroup);
					$pid=pdo_insertid();
				}
			}
			if(trim($item['jobtype'])=='全职'){
				$jobtype=0;
			}elseif(trim($item['jobtype'])=='兼职'){
				$jobtype=1;
			}elseif(trim($item['jobtype'])=='实习'){
				$jobtype=2;
			}
			if(trim($item['性别'])=='未知'){
				$sex=0;
			}elseif(trim($item['性别'])=='男'){
				$sex=1;
			}elseif(trim($item['性别'])=='女'){
				$sex=2;
			}
			$pcd=array('city'=>trim($item['城市名称']),'district'=>trim($item['地区名称']));
			$data=array(
				'city'=>trim($item['城市名称']),
				'district'=>trim($item['地区名称']),
				'pcd'=>ijson_encode($pcd),
				'name'=>trim($item['姓名']),
				'title'=>trim($item['意向职位']),
				'sex'=>$sex,
				'age'=>trim($item['出生年份']),
				'education'=>trim($item['最高学历']),
				'xueli'=>$this->xueli_type5[trim($item['最高学历'])],
				'express'=>trim($item['工作经验']),
				'address'=>trim($item['现居住地']),
				'email'=>trim($item['邮箱']),
				'mobile'=>trim($item['手机号']),
				'currentstatus'=>trim($item['目前状态']),
				'type'=>$jobtype,
				'pid'=>$pid,
			);
			//重复
			$isadd=pdo_get('jy_51job_jianli',array('name'=>$data['name'],'mobile'=>$data['mobile'],'city like'=>'%'.$data['city'].'%','title'=>$data['title']),array('id'));
			if($isadd['id']){
				$cdata[]=$data;
				continue;
			}
			$this->add($data,0,true,true);
		}
		return $cdata;
	}
}
?>